# coding:utf-8
import sys,os
import pandas as pd
from docxtpl import DocxTemplate
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QFileDialog
from qfluentwidgets import PushButton, PrimaryPushButton, LineEdit
from PyQt5.QtCore import Qt
from app.common.config import cfg

# enable dpi scale
if cfg.get(cfg.dpiScale) == "Auto":
    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
else:
    os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
    os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 初始化窗口
        self.setWindowTitle("房产评估报告生成器")
        self.setGeometry(100, 100, 800, 400)

        # 创建主Widget
        widget = QWidget()
        self.setCentralWidget(widget)

        # 布局
        layout = QVBoxLayout()

        # 通用参数输入
        self.assessor_input = LineEdit(self)
        self.assessor_input.setPlaceholderText("请输入评估人")
        layout.addWidget(self.assessor_input)

        self.address_input = LineEdit(self)
        self.address_input.setPlaceholderText("请输入房产地址")
        layout.addWidget(self.address_input)

        self.value_input = LineEdit(self)
        self.value_input.setPlaceholderText("请输入评估价值")
        layout.addWidget(self.value_input)

        # Excel文件选择
        self.excel_label = QLabel("请选择Excel文件")
        layout.addWidget(self.excel_label)

        self.select_excel_button = PrimaryPushButton("选择文件", self)
        self.select_excel_button.clicked.connect(self.select_excel_file)
        layout.addWidget(self.select_excel_button)

        # 生成报告按钮
        self.generate_button = PrimaryPushButton("生成报告", self)
        self.generate_button.clicked.connect(self.generate_report)
        layout.addWidget(self.generate_button)

        widget.setLayout(layout)

    def select_excel_file(self):
        options = QFileDialog.Options()
        file_path, _ = QFileDialog.getOpenFileName(self, "选择Excel文件", "", "Excel Files (*.xlsx);;All Files (*)",
                                                   options=options)
        if file_path:
            self.excel_label.setText(f"已选择文件: {file_path}")
            self.excel_file_path = file_path

    def generate_report(self):
        assessor = self.assessor_input.text()
        address = self.address_input.text()
        value = self.value_input.text()

        # 读取 Excel 数据
        data = self.read_excel(self.excel_file_path)

        # 手动输入数据结合 Excel 数据
        data['评估人'] = assessor
        data['房产地址'] = address
        data['评估价值'] = value

        # 使用模板生成报告
        self.create_word_report(data)

    def read_excel(self, file_path):
        df = pd.read_excel(file_path)
        data = df.to_dict(orient='records')[0]  # 假设第一行包含所需数据
        return data

    def create_word_report(self, data):
        template = DocxTemplate("template.docx")
        # 渲染模板，填充数据
        template.render(data)

        # 保存报告
        output_file = "房产评估报告.docx"
        template.save(output_file)

        print(f"报告已生成: {output_file}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
